using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UMC.Data;
using UMC.Web;


namespace UMC.WorkFlow.Subassembly
{

    /// <summary>
    /// 
    /// </summary>
    /// 
    [Apiumc("WorkFlow", "SqlSetting", Auth = WebAuthType.Admin, Desc = "配置脚本")]
    public class SqlSettingDesign : DesignActivity
    {


        protected override object SetValue(PropertyInfo property, object value, DesignAttribute attr)
        {
            if (property.PropertyType == typeof(SqlStatusSetting))
            {
                var sett = value as SqlStatusSetting ?? new SqlStatusSetting();
                var v = this.AsyncDialog("SqlSetting", g =>
                {
                    var form = new UIFormDialog() { Title = "SQL脚本" };
                    form.AddCheckBox("事件", "CommandType", "0")//.Put("保存", "1", (sett.CommandType & CommandType.Save) > 0)
                    .Put("申请", "2", (sett.CommandType & CommandType.Submit) > 0)
                    .Put("驳回", "4", (sett.CommandType & CommandType.Reject) > 0)
                    .Put("挂起", "8", (sett.CommandType & CommandType.Lock) > 0)
                    .Put("通过", "16", (sett.CommandType & CommandType.Approval) > 0)
                    .Put("加签", "64", (sett.CommandType & CommandType.Apostille) > 0)
                    .Put("终止", "32", (sett.CommandType & CommandType.Terminate) > 0)
                    .Put("完结", "512", (((int)sett.CommandType) & 512) > 0);
                    form.AddOption("数据库", "DbProviderKey", sett.DbProviderKey, sett.DbProviderKey).Command(this.Context.Request.Model, "DbProvider");
                    form.AddTextarea("SQL脚本", "SqlText", sett.SqlText).NotRequired().Put("tip", "");
                    if (String.IsNullOrEmpty(attr.Explain) == false)
                    {
                        form.AddFooter(attr.Explain);
                    }
                    form.Submit("确认",   "WorkFlow.Design");
                    return form;
                });
                sett.CommandType = CommandType.Undefine;
                var vs = v["CommandType"].Split(',');
                foreach (var c in vs)
                {

                    sett.CommandType = sett.CommandType | (CommandType)UMC.Data.Utility.EnumParse(c, CommandType.Undefine);
                }
                sett.DbProviderKey = v["DbProviderKey"];
                sett.SqlText = v["SqlText"] ?? String.Empty;
                if (String.Equals("none", sett.SqlText, StringComparison.CurrentCultureIgnoreCase))
                {
                    sett.SqlText = String.Empty;
                }
                return sett;


            }
            else
            {
                var sett = value as SqlSetting ?? new SqlSetting();
                var v = this.AsyncDialog("SqlSetting", g =>
                {
                    var form = new UIFormDialog() { Title = "SQL脚本" };
                    form.AddOption("数据库", "DbProviderKey", sett.DbProviderKey, sett.DbProviderKey).Command(this.Context.Request.Model, "DbProvider");
                    form.AddTextarea("SQL脚本", "SqlText", sett.SqlText).NotRequired().Put("tip", "");
                    if (String.IsNullOrEmpty(attr.Explain) == false)
                    {
                        form.AddFooter(attr.Explain);
                    }
                    form.Submit("确认",   "WorkFlow.Design");
                    return form;
                });
                sett.DbProviderKey = v["DbProviderKey"];
                sett.SqlText = v["SqlText"] ?? String.Empty;
                if (String.Equals("none", sett.SqlText, StringComparison.CurrentCultureIgnoreCase))
                {
                    sett.SqlText = String.Empty;
                }
                return sett;
            }
        }


    }

}
